package com.loadmore.ibook.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.loadmore.common.adapter.RVBaseAdapter;
import com.loadmore.common.adapter.RVBaseHolder;
import com.loadmore.common.bean.Book;
import com.loadmore.ibook.R;

/**
 * @author sunleilei
 * @date 2017/10/15
 * @time 下午11:19
 */

public class BookAdapter extends RVBaseAdapter {
    public BookAdapter(Context context) {
        super(context);
    }

    @Override
    public RVBaseHolder createHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.book_layout, parent, false);
        return new RVBaseHolder(view);
    }

    @Override
    public void bindHolder(RVBaseHolder holder, int position) {
        final Book book = (Book) items.get(position);
        holder.setImage(context, R.id.book_image, book.getImage())
                .setText(R.id.bool_title, book.getTitle())
                .setText(R.id.book_summary, book.getSummary());
    }

    public int queryBookIndex(String isbn) {
        for (int i = 0; i < items.size(); i++) {
            Book book = (Book) items.get(i);
            if (TextUtils.equals(book.getIsbn(), isbn)) {
                return i;
            }
        }
        return -1;
    }
}
